package main.controller;

import main.view.AdminView;
import main.service.VideoService;
import main.model.Video;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AdminController {
    private final AdminView view;
    private final VideoService videoService;

    public AdminController(AdminView view) {
        this.view = view;
        this.videoService = VideoService.getInstance();
        updateVideoList();

        // 传入已实现ActionListener的监听器
        view.addAddButtonListener(new AddButtonListener());
        view.addDeleteButtonListener(new DeleteButtonListener());
    }

    private void updateVideoList() {
        view.setVideoList(videoService.getAllVideos());
    }

    // 添加视频的监听器
    private class AddButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String title = JOptionPane.showInputDialog(view, "输入视频标题:");
            if (title != null && !title.trim().isEmpty()) {
                // 示例：添加视频到服务
                Video newVideo = new Video(
                        String.valueOf(System.currentTimeMillis()),
                        title,
                        "resources/videos/" + title.toLowerCase() + ".mp4"
                );
                videoService.addVideo(newVideo);
                updateVideoList();
            }
        }
    }

    // 删除视频的监听器
    private class DeleteButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            Video selectedVideo = view.getSelectedVideo();
            if (selectedVideo != null) {
                int confirm = JOptionPane.showConfirmDialog(
                        view,
                        "确认删除视频 '" + selectedVideo.getTitle() + "'?",
                        "警告",
                        JOptionPane.YES_NO_OPTION
                );
                if (confirm == JOptionPane.YES_OPTION) {
                    videoService.deleteVideo(selectedVideo.getId());
                    updateVideoList();
                }
            }
        }
    }
}